package com.edu.ujn.myh.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.edu.ujn.myh.domain.ArticleInfo;
import com.edu.ujn.myh.service.IApplyService;
import com.edu.ujn.myh.utils.FileOperation;
import com.edu.ujn.myh.utils.ZipUtils;

public class FileDownLoad extends BaseAction {

	private InputStream inputStream = null;

	private String fileName = "";

	private String filePath = "";

	private IApplyService applyService = null;
	
	private int articleId[];
	
	public String fileDownLoad() {
		// 获得要存储文件的目录路径
		String path = FileOperation.getFileSavePath(ServletActionContext
				.getRequest());
		if ("".equals(filePath)) {
			return ERROR;
		}
		path = path + "/" + filePath;
		File f = new File(path);
		if (f == null||!f.exists()) {
			return ERROR;
		}
		String name = f.getName();
		String str[] = name.split("&File&");
		name = str[1];
		try {
			fileName = new String(name.getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			return ERROR;
		}
		try {
			inputStream = new FileInputStream(f);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	  public String publishDownLoad(){
		  String path = FileOperation.getFileSavePath(ServletActionContext
					.getRequest());
//		  根据ID获得对应的文章列表，并将状态置为发表
		  if(articleId==null||articleId.length==0){
			  return ERROR;
		  }
		  List list = applyService.articleList(articleId);
		  if(list==null){
			  return ERROR;
		  }
		  List articleList = new ArrayList();
//		  生成zip文件
		  for(int i=0;i<list.size();i++){
			  ArticleInfo articleInfo = (ArticleInfo)list.get(i);
			  filePath = articleInfo.getFilePath();
			  String fileDes = path + "\\" + filePath;
				File f = new File(fileDes);
				if (f == null||!f.exists()) {
					continue;
				}
				articleList.add(f);
		  }
//		  将生产的zip写出去
		  String desFilePath = path+"/File.zip";
		  boolean flag = ZipUtils.zipFile(articleList, desFilePath);
		  if(!flag){
			  return ERROR;
		  }
			fileName = "File.zip";
			File f = new File(desFilePath);
			if(!f.exists()){
				 return ERROR;
			}
			try {
				inputStream = new FileInputStream(f);
			} catch (Exception e) {
				e.printStackTrace();
				return ERROR;
			}
		  return SUCCESS;
	  }
	public InputStream getInputStream() {
		return inputStream;
	}
	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public void setApplyService(IApplyService applyService) {
		this.applyService = applyService;
	}
	public void setArticleId(int[] articleId) {
		this.articleId = articleId;
	}
}
